<?
// SQLResult.php
// written by: David Fudge [ rkstar@mac.com ]
// created on: November 2, 2008
// last modified: November 2, 2008
//
// description:
// this class receives a database result and allows us to easily manipulate and
// retrieve info from it.

// load dependencies here:
ed::load("Core.Object");
ed::load("Core.Core");
ed::load("Core.Error");
ed::load("Data.Database");
ed::load("Data.SQLQuery");
ed::load("Utils.JSON");

class SQLResult
{
	private $result;
	private $query;
	private $queryType;
	private $link;
	private $dbType;
	private $affectedRows;
	private $lastInsertId;
	private $seek;
	private $limit;
	private $addVars;

	// constructor
	public function __construct( $result, $query, $link, $dbtype )
	{
		$this->result    = $result;
		$this->query     = $query;
		$this->link      = $link;
		$this->dbType    = $dbtype;
		$this->queryType = strtolower(substr($this->query,0,6));
		$this->seek      = -1;
		
		// set up the num rows or affected rows for the result
		switch( strtolower($this->dbType) )
		{
			case "mysql":
				$this->affectedRows = ($this->queryType=="select") ? @mysql_num_rows($this->result) : @mysql_affected_rows($this->link);
				$this->lastInsertId = @mysql_insert_id($this->link);
			break;
			
			case "mssql":
				$this->affectedRows = ($this->queryType=="select") ? @mssql_num_rows($this->result) : @mssql_rows_affected($this->link);
			break;
			
			case "odbc":
				$this->affectedRows = @odbc_num_rows($this->result);
			break;
		}
	}
	
	// retrieve the query
	public function getQuery() { return $this->query; }
	// get the query type
	public function getQueryType() { return $this->queryType; }
	// get the number of rows affected (numrows or affectedrows)
	public function numrows() { return $this->affectedRows; }
	// get the last insert id
	public function insertId() { return $this->lastInsertId; }
	// get the link identifier
	public function linkId() { return $this->link; }
	// set the iteration offset
	public function setOffset( $num ) { $this->seek = ($num > 0) ? $num : -1; }
	public function reset() { $this->setOffset(0); }
	public function getOffset() { return $this->seek; }
	// set the limit for iteration
	public function setLimit( $num ) { $this->limit = $num; }
	// reload the query by executing the sql again
	public function reload()
	{
		$db = new Database();
		$db->execute($this->query);
	}
	
	// add a name/value pair to each object that will be created when
	// cycling thru a result set
	public function addValue( $name, $value )
	{
		// sanity
		if( !is_array($this->addVars) ) { $this->addVars = array(); }
		// add the value to the array
		$this->addVars[$name] = $value;
	}
	
	// iterate thru the record set
	public function next()
	{
		// sanity
		if( !$this->result ) { return false; }
		
		// check the limit and increment our seek offset counter
		if( ($this->limit > 0) && ($this->limit == $this->seek) ) { return false; }
		$this->seek++;
		// get the data
		return $this->getResultData();
	}
	// iterate backwards thru the record set
	public function prev()
	{
		// sanity
		if( !$this->result ) { return false; }
		
		// decrement our counter
		$this->seek--;
		if( $this->seek < 0 ) { return false; }
		// get the data
		return $this->getResultData();
	}
	
	// retrieves the data from the result set and returns true or false
	private function getResultData()
	{
		// get the data
		switch( strtolower($this->dbType) )
		{
			case "mysql":
				// sanity
				if( !@mysql_data_seek($this->result,$this->seek) ) { return false; }
				if( !($row = @mysql_fetch_assoc($this->result)) ) { return false; }
			break;
			
			case "mssql":
				// sanity
				if( !@mssql_data_seek($this->result,$this->seek) ) { return false; }
				if( !($row = @mssql_fetch_assoc($this->result)) ) { return false; }
			break;
			
			case "odbc":
				// sanity
				if( !($row = @odbc_fetch_object($this->result)) ) { return false; }
			break;
		}

		// add the additional vars to the row
		if( is_array($this->addVars) ) { while( list($k,$v) = each($this->addVars) ) { $row[$k] = $v; } }
		// set the vars
		while( list($k,$v) = each($row) ) { $this->$k = $v; }
		
		return $this;
	}
	
	// JSONize the entire result set
	public function jsonize()
	{
		// instantiate our json class
		$json = new JSON();
		
		// jsonize our result set
		$jsontext = "";
		while( $this->next() ) { $jsontext .= $json->encode($this).","; }
		$jsontext = substr($jsontext,0,-1);
		
		// sanity
		if( $this->numrows() > 1 ) { $jsontext = "[".$jsontext."]"; }
		// return the json text
        return $jsontext;
	}
}
?>